package cn.edu.zut.cs.Controller;

import cn.edu.zut.cs.Business.CheckapplyService;
import cn.edu.zut.cs.Business.FirendsService;
import cn.edu.zut.cs.Model.Checkapply;
import cn.edu.zut.cs.Model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;

@Controller
public class CheckapplyController {
    @Autowired
    CheckapplyService checkapplyService;
    @Autowired
    FirendsService firendsService;

    @RequestMapping(value = "addfire",method = RequestMethod.POST)
    @ResponseBody
    public Object addfirendcheck(String requestper,String reponsedper){
        //将请求插入checkapply表中
        int i=checkapplyService.addfirendcheck(requestper,reponsedper);
        HashMap<String, String> hash = new HashMap<String, String>();
        if (i==1){
            hash.put("stateCode", "1");
        }
        return hash;
    }

    @RequestMapping(value = "showaddfire",method = RequestMethod.POST)
    @ResponseBody
    public List<User> showaddfire(String reponsedper){
        //查询出当前用户的checkapply
        //List<Checkapply> checkapplies=checkapplyService.query(reponsedper);
        //将申请者查询出来
        List<User> users=checkapplyService.queryByreponsedper(reponsedper);
        System.out.println(users);
        return users;
    }

    //通过好友验证
    @RequestMapping(value = "passcheck")
    public String  passcheck(@RequestParam String reponsedper,@RequestParam String requestper){
        //将申请表anwereper更新成 "验证通过"
        int i=checkapplyService.updateanwserper(requestper,reponsedper,"通过好友验证");
        firendsService.insertfriend(reponsedper,requestper);
        firendsService.insertfriend(requestper,reponsedper);
        return "findfriends";
    }

    //不通过好友验证
    @RequestMapping(value = "unpasscheck")
    public String  unpasscheck(@RequestParam String reponsedper,@RequestParam String requestper){
        //将申请表anwereper更新成 "未验证通过"
        int i=checkapplyService.updateanwserper(requestper,reponsedper,"未通过好友验证");
        return "findfriends";
    }
}
